package com.nebula.tlggoods.service.impl;

import com.nebula.tlggoods.mapper.CollectMapper;
import com.nebula.tlggoods.service.CollectService;
import com.nebula.tlggoods.tools.Record;
import com.nebula.tlggoods.tools.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 收藏实现类
 */
@Service
public class CollectServiceImpl implements CollectService {

    @Resource
    private CollectMapper collectMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 收藏商品，收藏店铺
     *
     * @param userId      用户id
     * @param collectType 收藏类型 0商品，1店铺
     * @param objectId    收藏对象的id
     * @return
     */
    @Transactional
    @Override
    public Record addCollect(Integer userId, Integer collectType, Integer objectId) {
        Record record = new Record();
        int count = 0;
        try {
            count = collectMapper.addCollect(userId, collectType, objectId);
        } catch (Exception e) {
            record.setStatus(Status.SAVE_FALIED);
            record.setMsg("收藏异常");
            e.printStackTrace();
            return record;
        }
        if (count > 0) {
            record.setStatus(Status.SAVE_SUCCESS);
            record.setMsg("收藏成功");
        } else {
            record.setStatus(Status.SAVE_FALIED);
            record.setMsg("收藏失败");
        }
        return record;
    }


    /**
     * 取消收藏
     *
     * @param userId      用户id
     * @param collectType 收藏类型 0商品，1店铺
     * @param objectId    收藏对象的id
     * @return
     */
    @Transactional
    @Override
    public Record cancelCollect(Integer userId, Integer collectType, Integer objectId) {
        Record record = new Record();
        int count = 0;
        try {
            count = collectMapper.cancelCollect(userId, collectType, objectId);
        } catch (Exception e) {
            record.setStatus(Status.UPDATE_FALIED);
            record.setMsg("取消收藏异常！！！");
            e.printStackTrace();
            return record;
        }
        if (count > 0) {
            record.setStatus(Status.UPDATE_SUCCESS);
            record.setMsg("取消收藏成功");
        } else {
            record.setStatus(Status.UPDATE_FALIED);
            record.setMsg("取消收藏失败！！");
        }
        return record;
    }

    /**
     * 判断商品、店铺是否已经收藏
     *
     * @param userId      用户id
     * @param collectType 收藏类型 0商品，1店铺
     * @param objectId    收藏对象的id
     * @return
     */
    @Override
    public Record isCollect(Integer userId, Integer collectType, Integer objectId) {
        Record record = new Record();
        int count = 0;
        try {
            count = collectMapper.isCollect(userId, collectType, objectId);
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("查询收藏异常");
            e.printStackTrace();
            return record;
        }
        if (count > 0) {
            record.setStatus(Status.SELECT_SUCCESS);
            record.setMsg("已收藏");
        } else {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("未收藏");
        }
        return record;
    }


}
